<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 15px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>第1步：选择商品类别</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="商品类别" prop="categoryId">
        <el-cascader
                style="width: 100%;"
                placeholder="请选择父级类别"
                v-model="ruleForm.categoryId"
                :options="categoryOptions"></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="$router.push('/sys-admin/product/album')">返回列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // 类别的下拉菜单的选项数据
        categoryOptions: [],
        ruleForm: {
          categoryId: '',
          categoryName: ''
        },
        rules: {

        }
      };
    },
    methods: {
      submitForm(formName) {
        if (Array.isArray(this.ruleForm.categoryId)) {
          this.ruleForm.categoryId = this.ruleForm.categoryId.pop();
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 发请求，查询类别详情，判断类别是否存在，类别是否包含子级类别，如果均没有问题，则跳转到下一步
            let url = 'http://176.212.26.33:8088/category/getStandard?id=' + this.ruleForm.categoryId;
            console.log('url = ' + url);

            let jwt=localStorage.getItem('jwt');

            this.axios.create({
              'headers':{
                'Authorization': jwt
              }
            }).get(url).then((response) => {
              let jsonResult = response.data;
              if (jsonResult.state == 2000) {
                let category = jsonResult.data;
                if (category.isParent == 1) {
                  let message = '错误：选择的类别必须不包含子级类别！';
                  this.$alert(message, '警告', {
                    confirmButtonText: '确定',
                    callback: action => {
                    }
                  });
                } else {
                  this.ruleForm.categoryName = category.name;
                  localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
                  this.$router.push('insert-2');
                }
              } else {
                this.$alert(jsonResult.message, '警告', {
                  confirmButtonText: '确定',
                  callback: action => {
                    this.loadCategoryTree();
                  }
                });
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      loadCategoryTree() {
        let url="http://176.212.26.33:8088/category/listTree"
        console.log('url = ' + url);
        let jwt=localStorage.getItem('jwt');
        this.axios.create({
          'headers':{
            'Authorization': jwt
          }
        }).get(url).then((response) => {
          let jsonResult = response.data;
          if (jsonResult.state == 2000) {
            this.categoryOptions = jsonResult.data;
            this.ruleForm.parentId = "0";
          }
        });
      }
    },
    mounted(){
      this.loadCategoryTree();
    }
  }
</script>